package com.mt.api.modules.mapstruct;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mt.common.utils.PloverDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: plover
 * @Date: 2022/01/13 4:57 下午
 * @Description:
 */
@Component
public class TransMapStruct {

    public String asString(Date date) {
        if (Objects.isNull(date)) {
            return "";
        }
        return PloverDateUtil.dateFormatyMdHms(date);
    }

    public Date asDate(String date) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        return DateUtil.parse(date, DatePattern.NORM_DATETIME_FORMAT).toJdkDate();
    }

    public String asStr(JSONObject object) {
        if (Objects.isNull(object)) {
            return null;
        }
        return JSON.toJSONString(object);
    }

    public List<String> strToList(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        return JSONArray.parseArray(str, String.class);
    }

    public String listToStr(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return JSONArray.toJSONString(list);
    }

}
